//
//  YbrTempPlaylistDao.m
//  wifibox
//
//  Created by os X 10.9 on 14-12-21.
//  Copyright (c) 2014年 yinbaner. All rights reserved.
//  临时的播放列表

#import "YbrTempPlaylistDao.h"
#import "YbrMusicItem.h"

@implementation YbrTempPlaylistDao

-(BOOL) setTemplistByArr:(NSArray *) arr {
    __block BOOL isOk = NO;
    
    [[YbrSQLHelper getDataBase] inTransaction:^(FMDatabase *db, BOOL *rollback) {
        [db executeUpdateWithFormat:@"delete from temp_playlist"];
        
        [db executeUpdateWithFormat:@"update sqlite_sequence set seq=0 where name='temp_playlist'"];
        
        for (int i = 0; i < arr.count; i ++) {
            YbrMusicItem *musicItem = arr[i];
            isOk = [db executeUpdate:@"insert into temp_playlist (name, singername ,albumname ,downloadurl ,file ,duration ,type ,timerstr ,musicid ,img) VALUES (?,?,?,?,?,?,?,?,?,?)",
                    musicItem.musicName ,
                    musicItem.singername ,
                    musicItem.albumname ,
                    musicItem.downloadurl ,
                    musicItem.file,
                    [NSNumber numberWithInt:musicItem.duration],
                    [NSNumber numberWithInt:musicItem.type] ,
                    musicItem.timeStr ,
                    [NSNumber numberWithInt:musicItem.musicId],
                    musicItem.img];
        }
    }];
    return isOk;
}


// 获取普通播放列表
-(NSArray *) getTemplist {
    __block NSArray *arr;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from temp_playlist"];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        while (rs.next) {
            YbrMusicItem *item = [[YbrMusicItem alloc] init];
            item.musicId = [rs intForColumn:@"musicid"];
            item.musicName = [rs stringForColumn:@"name"];
            item.singername = [rs stringForColumn:@"singername"];
            item.albumname = [rs stringForColumn:@"albumname"];
            item.downloadurl = [rs stringForColumn:@"downloadurl"];
            item.file = [rs stringForColumn:@"file"];
            item.img = [rs stringForColumn:@"img"];
            item.duration = [rs intForColumn:@"duration"];
            item.type = [rs intForColumn:@"type"];
            item.timeStr = [rs stringForColumn:@"timerstr"];
            [tempArr addObject:item];
        }
        [rs close];
        arr = [tempArr copy];
    }];
    return arr;

}
@end
